
using System;
using System.Linq;
using Csla;
using Csla.Data;
using Csla.Security;
using Csla.Validation;

namespace HousingInfo.Library
{
    public partial class UserBO : Csla.BusinessBase<UserBO>
    {

        #region Authorization Rules
        protected override void AddAuthorizationRules()
        {
            //TODO: Define authorization rules in UserBO
            //AuthorizationRules.AllowRead(UserNameProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(PasswordProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(FullNameProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(NickYahooProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(NickSkyperProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(TelePhoneProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(MobilePhoneProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(AddressProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(CityProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(UrlWebsiteProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(RegisterDateProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(QuestionIDProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(AnswerQuestionProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(IslockProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(LockDateProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(UnlockDateProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(RoleIDProperty, "UserBOReadGroup");
            //AuthorizationRules.AllowRead(IsActiveProperty, "UserBOReadGroup");

            //AuthorizationRules.AllowWrite(PasswordProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(FullNameProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(NickYahooProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(NickSkyperProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(TelePhoneProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(MobilePhoneProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(AddressProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(CityProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(UrlWebsiteProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(RegisterDateProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(QuestionIDProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(AnswerQuestionProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(IslockProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(LockDateProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(UnlockDateProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(RoleIDProperty, "UserBOWriteGroup");
            //AuthorizationRules.AllowWrite(IsActiveProperty, "UserBOWriteGroup");
        }

        protected static void AddObjectAuthorizationRules()
        {
            //TODO: Define object-level authorization rules of UserBO
            //AuthorizationRules.AllowCreate(typeof(UserBO), "UserBOCreateGroup"));
            //AuthorizationRules.AllowEdit(typeof(UserBO), "UserBOEditGroup"));
            //AuthorizationRules.AllowDelete(typeof(UserBO), "UserBODeleteGroup"));
        }
        #endregion //Authorization Rules
        private void AssignParentKey(HousingInfo.DataLinQ.User data)
        {
            //data.ParentId = ReadProperty<ParentBO>(ParentBOProperty).Id;
        }
        #region Kiem tra dang nhap
        public static bool CheckLogin(string userName, string passWord)
        {


            UserBO user = DataPortal.Fetch<UserBO>(new SingleCriteria<UserBO, string>(userName));
            if (user.Password == encryptData(passWord) && user.RoleID == 1)
                return true;
            else return false;

        }
        #endregion //Kiem tra dang nhap
        #region Ma Hoa Du Lieu trong Users
        public static string encryptData(string data)
        {
            var md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
            var encoder = new System.Text.UTF8Encoding();
            byte[] hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(data));
            var stringBuilder = new System.Text.StringBuilder();
            foreach (var b in hashedBytes)
            {
                stringBuilder.Append(b.ToString("x").ToLower());
            }
            return stringBuilder.ToString();
        }
        #endregion //Ma Hoa Du Lieu trong Users

        #region Kiem tra su ton tai cua username
        public static bool CheckUserNameExist(string username)
        {
            int count = 0;
            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                count = mgr.DataContext.Users.Count(u => u.UserName == username);
            }
            return (count > 0);

        }
        #endregion //Kiem tra su ton tai cua username

    }
}